package storeSorting

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"insighttracking/apps/sorting/auth"
	types2 "insighttracking/common/types"
	"insighttracking/query/service"
	"net/http"

	"insighttracking/apps/sorting/internal/svc"
	"insighttracking/apps/sorting/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type GetStoreSortingBatchNumberApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewGetStoreSortingBatchNumberApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *GetStoreSortingBatchNumberApiLogic {
	hostname, _ := os.Hostname()
	return &GetStoreSortingBatchNumberApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *GetStoreSortingBatchNumberApiLogic) GetStoreSortingBatchNumberApi(req *types.GetBatchNumberReq) (resp *types.GetBatchNumberResp, err error) {
	resp = &types.GetBatchNumberResp{}
	if req.SpuID == 0 && req.OrderID == 0 {
		return resp, errors.New("参数错误，至少要传一个ID")
	}

	accountInfo, err := auth.GetAccountInfo(l.svcCtx, l.Request)
	if err != nil {
		return
	}

	bo, err := service.ProductionPlanService.GetActivityDetailsBySpuID(l.ctx, accountInfo.EnterpriseId, req.SpuID)
	if err != nil {
		return
	}

	list := make([]*types2.SimpleList, 0)
	for _, item := range bo {
		list = append(list, &types2.SimpleList{
			ID:   item.ID,
			Name: item.Name,
			Code: item.Code,
		})
	}
	resp.List = list
	return
}
